﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;
using SocialShare.BLL;
using SocialShare.DAL.Facades;
using SocialShare.BusinessEntities;

namespace SocialShare.Web.Admin
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [WebMethod]
        public static String Report1()
        {
            List<CountReport> reports = new List<CountReport>();
            ForumFacade facade = new ForumFacade();
            List<MessageCategory> categories=facade.GetAllActiveForumCategory();

            foreach (MessageCategory category in categories)
            {
                CountReport rep = new CountReport();
                rep.Caption = category.CategoryName;
                rep.Total = category.TotalPost;
                reports.Add(rep);
            }

            StringBuilder strJson = new StringBuilder();
            strJson.AppendLine("[");
            foreach (CountReport rep in reports)
            {
                String str1 = SerializeObjectIntoJson(rep);
                strJson.AppendLine(str1);
                strJson.AppendLine(",");


            }
            String json = strJson.ToString();
            json = json.Substring(0, json.LastIndexOf(","));
            json = json + "]";

            return json;
        }

        [WebMethod]
        public static String Report2()
        {
            BlogFacade facade = new BlogFacade();
            Tuple<Int32, Int32> apprDisapprcCount=facade.GetApproveDisapprveTotal();
            Graph graph1 = new Graph();
            graph1.color = "#4572A7";
            graph1.name = "Published";
            graph1.y = apprDisapprcCount.Item1;
            String str1 = SerializeObjectIntoJson(graph1);

            Graph graph2 = new Graph();
            graph2.color = "#3EA99F";
            graph2.name = "Non Published";
            graph2.y = apprDisapprcCount.Item2;
            String str2 = SerializeObjectIntoJson(graph2);

            return @"[" + str1 + "," + str2 + "]";
        }

        [WebMethod]
        public static String Report3()
        {
            BlogFacade facade = new BlogFacade();
            List<CountReport> reports=facade.GetTotalPostsByMonth();
            StringBuilder strJson = new StringBuilder();
            strJson.AppendLine("[");
            foreach (CountReport rep in reports)
            {
                String str1 = SerializeObjectIntoJson(rep);
                strJson.AppendLine(str1);
                strJson.AppendLine(",");


            }
            String json = strJson.ToString();
            json = json.Substring(0, json.LastIndexOf(","));
            json = json + "]";

            return json;
        }

        [WebMethod]
        public static String Report4()
        {
            ForumFacade facade = new ForumFacade();
            List<CountReport> reports = facade.GetTotalPostsByMonth();
            StringBuilder strJson = new StringBuilder();
            strJson.AppendLine("[");
            foreach (CountReport rep in reports)
            {
                String str1 = SerializeObjectIntoJson(rep);
                strJson.AppendLine(str1);
                strJson.AppendLine(",");


            }
            String json = strJson.ToString();
            json = json.Substring(0, json.LastIndexOf(","));
            json = json + "]";

            return json;
        }
        private static string SerializeObjectIntoJson(Graph p)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, p);
                ms.Flush();
                byte[] bytes = ms.GetBuffer();
                string jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim('\0');
                return jsonString;
            }

        }

        private static string SerializeObjectIntoJson(CountReport p)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, p);
                ms.Flush();
                byte[] bytes = ms.GetBuffer();
                string jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length).Trim('\0');
                return jsonString;
            }
        }
    }
}